import { parse } from 'qs';
import AsyncStorage from '@react-native-community/async-storage';


export default {
  namespace: 'routerTab',
  state: {
    theme: '#3F51B5',//默认紫色
    defaultScreen: 'Tab',
    call: null,
    incomingCall: null,
    isScreenLocked: false,
    userId: '',
    isAnswer: false,
    isConnected: true,
    isAnotherLogin: 'N'
  },
  subscriptions: {
    setup({ dispatch }) {
    }
  },
  effects: {
    * initTheme({ payload }, { call, put, select }) {
      let theme = yield call(AsyncStorage.getItem, "theme_key");
      let isFlashShow = yield call(AsyncStorage.getItem, "isFlashShow");
      // alert("initTheme" + isFlashShow);
      if (!theme) {
        theme = '#3F51B5';
      }
      yield put({
        type: 'setTheme',
        payload: {
          theme: theme,
        },
      });
    },
    * initScreen({ payload }, { call, put, select }) {
      let userId = yield call(AsyncStorage.getItem, "userId");
      global.userId = userId;
      defaultScreen = 'Tab';
      if (defaultScreen != 'Tab')
        yield put({
          type: 'setDefaultScreen',
          payload: {
            defaultScreen: defaultScreen,
            userId: userId
          },
        });
    },
    * initConnectState({ payload }, { call, put, select }) {
      yield put({
        type: 'setConnectState',
        payload: {
          value: payload.value,
        },
      });
    },
  },
  reducers: {
    setTheme(state, action) {
      const theme = action.payload.theme;
      return { ...state, theme: theme };
    },
    setDefaultScreen(state, action) {
      const defaultScreen = action.payload.defaultScreen;
      const userId = action.payload.userId;
      return { ...state, defaultScreen: defaultScreen, userId };
    },
    setConnectState(state, action) {
      let isConnected = action.payload.value;
      return { ...state, defaultScreen: isConnected };
    },
  }
}
